/**
 * Created by yuris on 16.02.15.
 */
package controller {
import messages.ControllerMessage;

import model.PrototypeProxy;
import model.UserProxy;
import model.prototypes.ProductProto;
import model.prototypes.ShelfProto;
import model.prototypes.ShopDesignItemProto;

import mvcexpress.mvc.Command;

public class UserLevelChangedCommand extends Command {
    [Inject]
    public var prototypeProxy:PrototypeProxy;
    [Inject]
    public var userProxy:UserProxy;


    public function execute(param:Object):void {
        var _oldLevel:int = int(param.level_old);
        var _newLevel:int = int(param.level_new);

        var _newProductsToShow:Vector.<ProductProto> = new Vector.<ProductProto>();
        var _newFloorsShow:Vector.<ShopDesignItemProto> = new Vector.<ShopDesignItemProto>();
        var _newWallsShow:Vector.<ShopDesignItemProto> = new Vector.<ShopDesignItemProto>();
        var _newShelfs:Vector.<ShelfProto> = new Vector.<ShelfProto>();
        for (var i:int = _oldLevel+1; i <= _newLevel; i++) {
            _newProductsToShow = _newProductsToShow.concat(this._getProductsByLevel(i));
            _newFloorsShow = _newFloorsShow.concat(this._getFloorsByLevel(i));
            _newWallsShow = _newWallsShow.concat(this._getWallsByLevel(i));
            _newShelfs = _newShelfs.concat(this._getShelfsByLevel(i))
        }
        userProxy.addNewAvailableProductsToOrder(_newProductsToShow);
        userProxy.addNewAvailableFloors(_newFloorsShow);
        userProxy.addNewAvailableWalls(_newWallsShow);
        userProxy.addNewAvailableShelfs(_newShelfs);

    }

    private var _shelfsPrototypes:Vector.<ShelfProto>;
    private var _shelfProtoBuf:ShelfProto;
    private function _getShelfsByLevel(level:int):Vector.<ShelfProto> {
        var _result:Vector.<ShelfProto> = new <ShelfProto>[];
        this._shelfsPrototypes = prototypeProxy.shelfPrototypes;
        for (var i:int = 0; i < _shelfsPrototypes.length; i++) {
            this._shelfProtoBuf = _shelfsPrototypes[i];
            if(this._shelfProtoBuf.level == level){
                _result.push(_shelfProtoBuf);
            }
        }
        return _result;
    }

    private var _floorsPrototypes:Vector.<ShopDesignItemProto>;
    private var _shopDesignItemProtobuf:ShopDesignItemProto;
    private function _getFloorsByLevel(level:int):Vector.<ShopDesignItemProto> {
        var _result:Vector.<ShopDesignItemProto> = new <ShopDesignItemProto>[];
        this._floorsPrototypes = prototypeProxy.floorsPrototypes;
        for (var i:int = 0; i < _floorsPrototypes.length; i++) {
            this._shopDesignItemProtobuf = _floorsPrototypes[i];
            if(this._shopDesignItemProtobuf.level == level){
                _result.push(_shopDesignItemProtobuf);
            }
        }
        return _result;
    }

    private var _wallsPrototypes:Vector.<ShopDesignItemProto>;
    private function _getWallsByLevel(level:int):Vector.<ShopDesignItemProto> {
        var _result:Vector.<ShopDesignItemProto> = new <ShopDesignItemProto>[];
        this._wallsPrototypes = prototypeProxy.wallsPrototypes;
        for (var i:int = 0; i < _wallsPrototypes.length; i++) {
            this._shopDesignItemProtobuf = _wallsPrototypes[i];
            if(this._shopDesignItemProtobuf.level == level){
                _result.push(_shopDesignItemProtobuf);
            }
        }
        return _result;
    }


    private var _productPrototypes:Vector.<ProductProto>;
    private var _productProtoBuf:ProductProto;
    private function _getProductsByLevel(level:int):Vector.<ProductProto> {
        var _result:Vector.<ProductProto> = new <ProductProto>[];
        this._productPrototypes = prototypeProxy.productsPrototypes;
        for (var i:int = 0; i < this._productPrototypes.length; i++) {
            this._productProtoBuf = this._productPrototypes[i];
            if(this._productProtoBuf.level == String(level)){
                _result.push(this._productProtoBuf);
            }
        }
        return _result;
    }
}
}
